// pages/lock/lock.js
import {
	request
} from '../../utils/api.js'
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		mask: false,
		alert: false,
		alert1: false,
		show: {
			company: {},
			lock: {}
		},
		userinfo: {},
		company: {},
		lock: {}
	},
	examine(e){
		if(!e.detail.value){
			this.setData({
				'show.company.id':null
			})
		}
	},
	examine1(e){
		if(!e.detail.value){
			this.setData({
				'show.lock.id':null
			})
		}
	},
	close() {
		this.setData({
			mask: false,
			alert: false,
			alert1: false
		})
	},
	bindcompany(e) {
		this.getcompanylist(e);
	},
	toindex() {

		if (!this.data.show.company.id) {
			wx.showModal({
				content: '请选择单位！'
			})
			return;
		}
		if (!this.data.show.lock.id) {
			wx.showModal({
				content: '请选择门锁！'
			})
			return;
		}

		app.globalData.info = this.data.show;
		console.log(this.data.show)
		wx.navigateBack({});
	},
	bindlock(e) {
		if (!this.data.show.company.id) {
			wx.showModal({
				content: '请选择单位！'
			})
			return;
		}
		this.search_lock(e);

	},
	search_lock(e) {
		
		wx.showLoading({
			title: '正在搜索',
		})
		request({
			url: '/api/wxapi/search_lock',
			header: {
				token: wx.getStorageSync('token')
			},
			data: {
				admin_id: this.data.show.company.id,
				keywords: e.detail.value
			}
		}).then(res => {
			wx.hideLoading();
			if (res.data.data.length != 0) {
				this.setData({
					lock: res.data.data
				})
				this.setData({
					mask: true,
					alert1: true
				})
			} else {
				this.setData({
				   'show.lock.id':null
				})
				wx.showModal({
					content: '暂无数据'
				})


			}

		})
	},
	getcompanylist(e) {
		
		wx.showLoading({
			title: '正在搜索',
		})
		request({
			url: '/api/wxapi/search_cuscomter',
			header: {
				token: wx.getStorageSync('token')
			},
			data: {
				keywords: e.detail.value
			}
		}).then(res => {
			wx.hideLoading();
			if (res.data.data.length != 0) {

				this.setData({
					company: res.data.data
				})
				this.setData({
					mask: true,
					alert: true
				})
			} else {
				this.setData({
				   'show.company.id':null
				})
				wx.showModal({
					content: '暂无数据'
				})
			}

		})
	},
	chosscompay(e) {
		this.setData({
			mask: false,
			alert: false,
			'show.company': e.currentTarget.dataset.name
		})
	},
	close() {
		this.setData({
			mask: false,
			alert: false,
			alert1: false,
		})
	},
	chooselock(e) {
		this.setData({
			mask: false,
			alert1: false,
			'show.lock': e.currentTarget.dataset.name
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

		this.setData({
			userinfo: JSON.parse(options.user)
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
